/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.utils;

import common.Articulo;
import common.ArticuloStock;
import common.ItemTabla;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import tiendagui.internal.JDialogArticulo;

/**
 *
 * @author Jhonny
 */
public class ManagerTabla {

    private JTable              tabla;
    private int                 columnId;
    private ControllerTable     controller;
//    private JButton     agregar;
//    private JButton     eliminar;
    public ManagerTabla(JTable tabla,JButton agregar,JButton eliminar) {
        initTabla(tabla);
        if(agregar!=null){
            //this.agregar = agregar;
            agregar.addActionListener(new AgregarListener());
        }
        if(eliminar !=null){
            //this.eliminar = eliminar;
            eliminar.addActionListener(new EliminarListener());
        }
    }

    public void setColumnId(int columnId) {
        this.columnId = columnId;
    }

    
    public List<Integer> getListReferencia(){
        List<Integer> list = new LinkedList<Integer>();
        int rowCount = ((DefaultTableModel)tabla.getModel()).getRowCount();
        for (int i = 0; i < rowCount; i++) {
            list.add(getReferencia(i));
        }
        return list;
    }
    public List<ItemTabla> getListItems() {
        List<ItemTabla> list = new LinkedList<ItemTabla>();
        int rowCount = ((DefaultTableModel)tabla.getModel()).getRowCount();
        for (int i = 0; i < rowCount; i++) {
            ItemTabla item = getObject(i);
            if(item!=null){
                list.add(item);
            }
        }
        return list;
    }
    public ManagerTabla(JTable tabla) {
        initTabla(tabla);
    }
    private void initTabla(JTable tabla){
        columnId = 0;
        if(tabla!=null){
            this.tabla = tabla;
            tabla.addMouseListener(new TablaListener());
        }
    }
    public void setController(ControllerTable controller) {
        this.controller = controller;
    }

    public void mouseClicked(java.awt.event.MouseEvent evt) {

    }
    protected Integer getReferencia(int row) {
            Object obj = null;
            Integer referencia=null;
            obj = ((DefaultTableModel)tabla.getModel()).getValueAt(row, columnId);
            if(obj!=null){
                if(obj instanceof Integer){
                    referencia = (Integer) obj;
                }else if (obj instanceof Articulo){
                    referencia = ((Articulo) obj).getReferencia();
                }
            }
            return referencia ;
        }

    private ItemTabla getObject(int row) {
        Object obj = null;
        ItemTabla item= null;
        obj = ((DefaultTableModel)tabla.getModel()).getValueAt(row, columnId);
        if(obj!=null){
            if (obj instanceof ArticuloStock){
                item = (ItemTabla) obj;
            }
        }
        return item ;
    }



    class TablaListener implements MouseListener{

        private JDialogArticulo dialog;
        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount()==2){
                int row = tabla.getSelectedRow();
                if(row >=0){
                    Integer referencia = getReferencia(row);
                    Articulo articulo = controller.getArticulo(referencia);
                    if(articulo!=null){
                        getDialog().setArticulo(articulo);
                        getDialog().setVisible(true);
                    }else{
                        JOptionPane.showMessageDialog(dialog, "No se encontro el Articulo de referencia "+referencia);
                    }

                }
            }
        }

        public JDialogArticulo getDialog() {
            if(dialog==null){
                dialog = new JDialogArticulo(null, true);
            }
            return dialog;
        }


        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }

       

    }
    class AgregarListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            ItemTabla item = controller.getArticulo();
            if(item!=null){
                if(noEstaEnTabla(item.getId())){
                    ((DefaultTableModel)tabla.getModel()).addRow(item.toVector());
                }
            }
        }

        private boolean noEstaEnTabla(int referencia) {
            boolean encontrado=false;
            int rowCount = ((DefaultTableModel)tabla.getModel()).getRowCount();
            for (int i = 0; i < rowCount; i++) {
                if(((DefaultTableModel)tabla.getModel()).getValueAt(i, columnId).equals(referencia)){
                    encontrado = true;
                    break;
                }

            }
            return encontrado;
        }

    }
    class EliminarListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int row = tabla.getSelectedRow();
            if(row >=0){
                ((DefaultTableModel)tabla.getModel()).removeRow(row);
            }else{
                JOptionPane.showMessageDialog(null, "Debe seleccionar una fila de la tabla.");
            }
        }

    }
}
